<?php

namespace App\Http\Controllers\Api;

use App\Librarys\Message;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends BaseController
{
    protected Request $request;

    public function __construct(Request $request)
    {
        $this->request = $request;   
    }


    public function upload_2_cloud_flare()
    {
        // 获取文件
        $file = $this->request->file('file');
        // 获取当前上传目录
        $dir = $this->request->dir;
        // 捕捉异常
        try{
            // put文件到服务器
            $path = Storage::disk('s3')->put($dir . '/' . date('Y-m-d'), $file);
            return Message::upload(0, '上传成功', env('CLOUDFLARE_R2_URL') . $path);
        }catch(Exception $error) {
            return Message::show(false, '上传失败：' . $error->getMessage());
        }
        
    }
}
